package com.hackordie.daos;

import com.hackordie.commons.hardware.computer.ComputerType;
import com.hackordie.daos.mappers.ComputerTypeMapper;

public class ComputerTypeDao extends ACommonDao<ComputerType> {

    @Override
    public boolean create(ComputerType entity) {
        final String SQL = "INSERT INTO computerType (name, " +
        		                                     "maxFirewalls, " +
        		                                     "maxHardDisks, " +
        		                                     "maxNetworkCards, " +
        		                                     "maxPowerSupplies, " +
        		                                     "maxProcessors, " +
        		                                     "maxRams, " +
        		                                     "price) " +
        		                   "VALUES ('" + entity.getName() + "', " +
        		                            entity.getMaxFirewalls() + ", " +
        		                            entity.getMaxHardDisks() + ", " + 
        		                            entity.getMaxNetworkCards() + ", " + 
        		                            entity.getMaxPowerSupplies() + ", " + 
        		                            entity.getMaxProcessors() + ", " +
        		                            entity.getMaxRams() + ", " +
        		                            entity.getPrice() + ");";
        
        if(jdbcTemplate.update(SQL) == 1) {
            return true;
        }
        return false;
   
    }

    @Override
    public boolean delete(int id) {
        final String SQL = "DELETE FROM computerType WHERE idComputer = ?";
        if(jdbcTemplate.update(SQL, id) == 1) {
            return true;
        }
        return false;
    }
    
    public boolean delete(String name) {
        final String SQL = "DELETE FROM computerType WHERE name = ?";
        if(jdbcTemplate.update(SQL, name) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean update(ComputerType entity) {
        final String SQL = "UPDATE computerType " +
        		           "SET name = '" + entity.getName() + "', " +
        		                "maxFirewalls = "     + entity.getMaxFirewalls()     + ", " +
        		                "maxHardDisks = "     + entity.getMaxHardDisks()     + ", " +
        		                "maxNetworkCards = "  + entity.getMaxNetworkCards()  + ", " + 
        		                "maxPowerSupplies = " + entity.getMaxPowerSupplies() + ", " +
        		                "maxProcessors = "    + entity.getMaxProcessors()    + ", " +
        		                "maxRams = "          + entity.getMaxRams()          + ", "  +
        		                "price = "            + entity.getPrice()            + " "  +
        		           "WHERE idComputer = "      + entity.getIdComputer();
                                             
        if(jdbcTemplate.update(SQL) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public ComputerType[] getAll() {
        final String SQL = "SELECT * FROM computerType";
        return jdbcTemplate.query(SQL, new ComputerTypeMapper()).toArray(new ComputerType[] {});
    }

    @Override
    public ComputerType get(int id) {
        final String SQL = "SELECT * FROM computerType WHERE idComputer = ?";
        return jdbcTemplate.queryForObject(SQL, new ComputerTypeMapper(), id);
    }
    
    public ComputerType get(String name) {
        final String SQL = "SELECT * FROM computerType WHERE name = ?";
        return jdbcTemplate.queryForObject(SQL, new ComputerTypeMapper(), name);
    }

    public ComputerType[] getByRange(int first, int last) {
        final String SQL = "SELECT * FROM computerType LIMIT ?,?";
        return jdbcTemplate.query(SQL, new ComputerTypeMapper(), first, last).toArray(new ComputerType[] {});
    }

    public int countAll() {
        final String SQL = "Select COUNT(idComputer) FROM computerType";
        return jdbcTemplate.queryForObject(SQL, Integer.class);
    }
}
